package com.usian.controller;

import com.usian.Result;
import com.usian.feign.CartFeign;
import com.usian.feign.OrderFeign;
import com.usian.pojo.OrderInfo;
import com.usian.pojo.TbItem;
import com.usian.pojo.TbOrder;
import com.usian.pojo.TbOrderShipping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("frontend/order")
public class OrderController {

    @Autowired
    private CartFeign cartFeign;

    @Autowired
    private OrderFeign orderFeign;

    /**
     * 进入订单确认页
     *
     * @param ids
     * @param userId
     * @param token
     * @return
     */
    @RequestMapping("goSettlement")
    public Result goSettlement(String[] ids, String userId,String token){

        //1：从redis取用户的购物车
        Map<String, TbItem> cart = cartFeign.selectCartByUserId(userId);
        //从购物车中获取选中的商品
        List<TbItem> list = new ArrayList<TbItem>();
        for (String id : ids) {
            list.add(cart.get(id));
        }
        if(list.size()>0) {
            return Result.ok(list);
        }
        return Result.error("error");

    }

    /**
     * 创建订单
     *
     * @param orderItem
     * @param tbOrder
     * @param tbOrderShipping
     * @return
     */
    @RequestMapping("insertOrder")
    public Result insertOrder(String orderItem, TbOrder tbOrder, TbOrderShipping tbOrderShipping){
        OrderInfo orderInfo =new OrderInfo();
        orderInfo.setTbOrder(tbOrder);
        orderInfo.setTbOrderShipping(tbOrderShipping);
        orderInfo.setOrderItem(orderItem);

        Long orderId = orderFeign.insertOrder(orderInfo);
        if(orderId!=null){
            return Result.ok(orderId);
        }
        return Result.error("创建订单失败");
    }
}
